package com.example.taximeter.service.impl;

import com.example.taximeter.bean.Company;
import com.example.taximeter.bean.Fact;
import com.example.taximeter.bean.ReturnInfo;
import com.example.taximeter.mapper.FactMapper;
import com.example.taximeter.mapper.ParamFileMapper;
import com.example.taximeter.mapper.TaximeterMapper;
import com.example.taximeter.service.FactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计价器厂商
 */
@Service
public class FactServiceImpl implements FactService {

    private FactMapper factMapper;
    private TaximeterMapper taximeterMapper;
    private ParamFileMapper paramFileMapper;

    @Autowired
    public FactServiceImpl(FactMapper factMapper,TaximeterMapper taximeterMapper,ParamFileMapper paramFileMapper){
        this.factMapper = factMapper;
        this.taximeterMapper = taximeterMapper;
        this.paramFileMapper = paramFileMapper;
    }

    @Override
    public List<Fact> findFactList() {
        return factMapper.findFactList();
    }

    @Override
    public String findFactnameByFactid(Integer id) {
        return factMapper.findFactnameByFactid(id);
    }

    @Override
    public ReturnInfo findFactAll(Integer pageNum, Integer pageSize, Fact fact) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<Fact> facts = factMapper.findFactAll(fact);
        PageInfo<Fact> pageInfo = new PageInfo<>(facts);
        if(facts.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public String addFact(Fact fact) {
        if(factMapper.findFactByFactname(fact.getFactname()).size()!=0){
            return "计价器厂商名称重复";
        }else if(factMapper.addFact(fact)){
            return "成功";
        }else {
            return "失败";
        }

    }

    @Override
    public String updateFact(Fact fact) {
        List<Fact> facts = factMapper.findFactByFactname(fact.getFactname());
        if(facts.size()==1){
            if(facts.get(0).getId()!=fact.getId()){
                return "计价器厂商名称重复";
            }else {
                if(factMapper.updateFact(fact)){
                    return "成功";
                }else {
                    return "失败";
                }
            }
        }else if(facts.size()>1){
            return "计价器厂商名称重复";
        }else {
            if(factMapper.updateFact(fact)){
                return "成功";
            }else {
                return "失败";
            }
        }
    }



    @Override
    public String deleteFact(Integer id) {
        if(taximeterMapper.findTaximeterByFactid(id).size()>0 || paramFileMapper.findParamFileByFactid(id).size()>0){
            return "该计价器厂商已被引用";
        }else {
            if (factMapper.deleteFact(id)) {
                return "成功";
            } else {
                return "失败";
            }
        }
    }

}
